/**
 * Stundent.java, extends Person
 * 
 * This class represents a student.
 *
 * @author Felix Schuller
 */

package Runde2;

import java.util.HashMap;
import java.util.HashSet;

public class Student extends Person{

	private HashMap<Integer,Integer> grades;
	private HashMap<Integer,Integer> tries;
	private HashSet<Activity> authorizedActivities;
	
	/**
	 * Creates a new student.
	 * 
	 * @params see Person
	 * @throws InvalidPersonException - thrown if fName/lName/password is a empty string
	 */
	public Student(String fName, String lName, String password) throws InvalidPersonException {
		super(fName, lName, password);
		grades = new HashMap<Integer, Integer>();
		tries = new HashMap<Integer, Integer>();
		authorizedActivities = new HashSet<Activity>();
	}
	
	/**
	 * Authorizes given activity for grading
	 * 
	 * @param act - activity to authorize
	 * @param pw - student's password
	 * @throws InsufficientPermissionsException - thrown if wrong password was given
	 */
	public void authorizeForGrading(Activity act, String pw) throws InsufficientPermissionsException {
		if (!validate(pw)){
			throw new InsufficientPermissionsException("Permission denied. (Wrong student's password given.)");
		}
		authorizedActivities.add(act);
	}
	
	/**
	 * Sets the student's grade
	 * 
	 * @param act - activity that wants to set grade
	 * @param grade - grade
	 * @throws InsufficientPermissionsException - thrown if unknown unauthorized activity is given
	 */
	public void setGrade(Activity act, int grade) throws InsufficientPermissionsException {
		if (!authorizedActivities.contains(act)) {
			throw new InsufficientPermissionsException("Given activity is not allowed to set grade. Is student participant of the activity?");
		}
		
		int lvaNr = act.getLVA();
		grades.put(lvaNr, grade);
		if (!tries.containsKey(lvaNr)){
			tries.put(lvaNr, 0);
		} 
		tries.put(lvaNr, tries.get(lvaNr) + 1);	
		
		authorizedActivities.remove(act);
	}

	/**
	 * Returns the student's tries for the given LVA
	 * 
	 * @param lvaNr - LVA-number
	 * @return tries - tries
	 */
	public int getTries(int lvaNr){
		if (!tries.containsKey(lvaNr)){
			return 0;
		}
		return tries.get(lvaNr);
	}
	
	/**
	 * Returns true or false whether the student has completed the given array of LVA(-Nr)s or not
	 * 
	 * @param lvaNr - LVA-number
	 * @return true / false -  whether the student has completed the given array of LVA(-Nr)s or not
	 */
	public boolean validatePassedLVAs(int[] lvaNrArr) {
		for (int i = 0; i < lvaNrArr.length;i++){
			if (!grades.containsKey(lvaNrArr[i])){
				return false;
			}
			if (grades.get(lvaNrArr[i]) == 5){
				return false;
			}
		}
		return true;
	}
	
	
}
